import { getJobs, getMaterial } from "@/api/system/dict"
import {getNoExit as getProject} from "@/api/project.js"
const state = {
	jobs:[],
	projects:[],
	materials:[]
}

const mutations = {
	SET_JOBS:(state, jobs) => {
		state.jobs = jobs
	},
	SET_PROJECT:(state, projects) => {
		state.projects = projects
	},
	SET_MATERIAL:(state, materials) => {
		state.materials = materials
	},
}
const actions = {
	setJobs({ commit }, data) {
	  commit('SET_JOBS', data)
	},
	getJobs({commit, state}){
		if (state.jobs.length>0){
			return Promise.resolve(state.jobs)
		} else {
			return new Promise((resolve, reject)=>{
				getJobs().then(res=>{
					let _data = res.data
					_data.forEach(d=>{
						d.label = d.workJobType
						d.value = d.id
					})
					commit('SET_JOBS', _data)
					resolve(_data)
				})
			})
		}
	},
	getMaterials({commit, state}){
		if (state.materials.length>0){
			return Promise.resolve(state.materials)
		} else {
			return new Promise((resolve, reject)=>{
				getMaterial().then(res=>{
					let _data = res.data
					_data.forEach(d=>{
						d.label = d.name
						d.value = d.id
					})
					commit('SET_MATERIAL', _data)
					resolve(_data)
				})
			})
		}
	},
	getProjects({commit, state}){
		return new Promise((resolve, reject)=>{
			getProject({pageSize:100, pageNum:1}).then(res=>{
				let _data = res.data
				_data.forEach(d=>{
					d.label = d.projectName
					d.value = d.id
				})
				commit('SET_PROJECT', _data)
				resolve(_data)
			})
		})
		// if (state.projects.length>0){
		// 	return Promise.resolve(state.projects)
		// } else {
		// 	return new Promise((resolve, reject)=>{
		// 		getProject({pageSize:100, pageNum:1}).then(res=>{
		// 			let _data = res.data
		// 			_data.forEach(d=>{
		// 				d.label = d.projectName
		// 				d.value = d.id
		// 			})
		// 			commit('SET_PROJECT', _data)
		// 			resolve(_data)
		// 		})
		// 	})
		// }
	},
}

export default {
	namespaced: true,
	state,
	 mutations,
	 actions
}